import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:yp/pages/mall_app/invalid_account/account_invalid_2/view.dart';

import '../../../../helper.dart';
import 'state.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/pages/mall_app/account_safe_modify_phone.dart/state.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';

class AccountInvalid1Logic extends GetxController {
  final AccountInvalid1State state = AccountInvalid1State();
  @override
  void onInit() {
    super.onInit();
    var _mobile = SpUtil.getString('number');
    if (_mobile.length > 4) {
      var sufixLength = (_mobile.length - 4) ~/ 2;
      var prefixLength = _mobile.length - 4 - sufixLength;
      state.phone = _mobile.substring(0, prefixLength) +
          '****' +
          _mobile.substring(_mobile.length - sufixLength, _mobile.length);
    } else {
      state.phone = _mobile;
    }
  }

  //请求发送验证码
  void sendSms() {
    if (!state.verifyEnable) {
      return;
    }
    state.verifyEnable = false;
    var params = {
      "mobile": SpUtil.getString('number'),
      "verify_code_type": "logout"
    };
    Api.sendSms(params).listen((event) {
      if (event['code'] == 0) {
        toast("验证码已发送,请注意查收");
        state.smsData = event;
      } else {
        toast(event['message']);
        state.verifyEnable = true;
      }
      state.focusNode.canRequestFocus = true;
      update();
    });

    state.timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (state.seconds == 0) {
        state.timer?.cancel();
        state.verifyText = '重新发送';
        state.seconds = 60;
        state.verifyEnable = true;
        state.focusNode.canRequestFocus = true;
        update();
        return;
      }
      state.verifyEnable = false;
      state.seconds = --state.seconds;
      state.verifyText = "已发送${state.seconds}s";
      print("已发送${state.seconds}s");
      update();
    });
  }

  netStep() {
    if (state.textCtrl.text.length == 0) {
      toast('请输入验证码');
      return;
    }
    if (state.smsData == null) {
      toast('请先获取验证码');
      return;
    }
    state.verifyEnable = false;
    showDialog(
        context: Get.context,
        builder: (context) {
          return CupertinoAlertDialog(
            content: Text(
              '确定注销当前账户?',
              style:
                  TextStyle(color: Color(0xff333333), fontSize: setWidth(30)),
            ),
            actions: [
              CupertinoDialogAction(
                child: Text(
                  "取消",
                  style: TextStyle(color: Color(0xff666666)),
                ),
                onPressed: () {
                  Get.back();
                  print("取消");
                },
              ),
              CupertinoDialogAction(
                child: Text("确定"),
                onPressed: () {

                  var params = {
                    "usr_num_id": SpUtil.getInt('empeNumId'),
                    "mobile": SpUtil.getString('number'),
                    'sms_verify_code': state.textCtrl.text,
                    'sms_verify_token': state.smsData['token'],
                    "sms_verify_type": "logout"
                  };
                  print(params);
                  Api.accountInvalid(params).listen((event) {
                    if (event['code'] == 0) {
                      logout();
                    } else {
                      toast(event['message']);
                    }
                    update();
                  });

                  // Get.back();
                  // Get.offAndToNamed(RouteConfig.accountInvalid2, parameters: {
                  //   'code': state.textCtrl.text,
                  //   'token': state.smsData['token']
                  // });
                },
              ),
            ],
          );
        });
  }

  @override
  void dispose() {
    super.dispose();
    state.timer.cancel();
  }
  /// 退出登陆
  void logout() {
    // SpUtil.remove(Config.TOKEN);
    // SpUtil.remove(Config.USER_CACHE);
    // SpUtil.remove('sid');
    var exist = SpUtil.getString("yes") == "a";
    SpUtil.clear();
    if (exist) {
      SpUtil.putString("yes", "a");
    }
    Get.offAllNamed(RouteConfig.mallLogin);
    update();
  }
}
